package com.example.demo;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Stream流
 * 参考最全的Stream：https://mp.weixin.qq.com/s/QCqYEvWVIoWrhlmmiayCJg
 */
public class StreamTest {



    public static void main(String[] args) {



        Map<String, String> map = new HashMap<>();
         map.put("01", "1");
        map.put("02", "3");
        map.put("03", "3");
        map.put("04", "4");
        List<SsopStatisData> ssopStatisDataList = new ArrayList<>();

        if (map != null && map.size() > 0) {
            for (String s : map.keySet()) {
                SsopStatisData ssopStatisData = new SsopStatisData();
                if ("01".equals(s)){
                    ssopStatisData.setName("windows");

                }else {
                    ssopStatisData.setName(s);

                }
                ssopStatisData.setValue(Integer.valueOf(map.get(s)));
                ssopStatisDataList.add(ssopStatisData);
            }
        }
        if (ssopStatisDataList.size() >2){
            List<SsopStatisData> subList = ssopStatisDataList.subList(0, 2);
            System.out.println("切割后的分组统计返回结果"+JSONObject.toJSONString(subList));

        }



 /*       Apple apple1 = new Apple();
        apple1.setId(1);
        apple1.setName("苹果11");
        apple1.setNum(123);
        System.out.println(apple1);

        User user = new User();
        user.setName("张阿三");
        List list = new ArrayList();
        list.add(apple1);
        list.add(user);
        for (int i = 0; i <list.size() ; i++) {
            Object o = list.get(i);
            Map<String, String> stringStringMap = ObjectToMap(o);
            String name = stringStringMap.get("name");
            System.out.println(name);

        }*/
       /* String  str = "{\"created\":0,\"creatorId\":0,\"preset\":true,\"leaf\":true,\"parentId\":0,\"parentKey\":\"0\",\"name\":\"总部\",\"modified\":0,\"id\":1,\"passing\":false,\"position\":\"\",\"shortName\":\"\",\"displayIndex\":0,\"key\":\"1\",\"group\":false,\"levelcode\":\"/1\"}";
        Map<? ,?> mapType = JSON.parseObject(str,Map.class);
        SsopDictValue ssopDictValue = new SsopDictValue();
        Object id = mapType.get("id");
        String s = id.toString();
        ssopDictValue.setId(Long.valueOf(id.toString()).longValue());
        Object position = mapType.get("position");
        if (position != null) {
            String s1 = position.toString();
            System.out.println(s1);
        }*/
        /*List<Apple> appleList = new ArrayList();//存放apple对象集合

        Apple apple1 = new Apple(1, "苹果1", new BigDecimal("3.25"), 10);
        Apple apple12 = new Apple(1, "苹果2", new BigDecimal("1.35"), 20);
        Apple apple2 = new Apple(2, "香蕉", new BigDecimal("2.89"), 30);
        Apple apple3 = new Apple(3, "荔枝", new BigDecimal("9.99"), 40);

        appleList.add(apple1);
        appleList.add(apple12);
        appleList.add(apple2);
        appleList.add(apple3);


        *//**
         * list 转map
         *//*

        *//*Map<Integer, String> collect1 = appleList.stream().collect(Collectors.toMap(Apple::getId, Apple::getName));
        System.out.println("applle"+collect1);*//*
        //applle{1=苹果1, 2=香蕉, 3=荔枝, 4=苹果2}

        List<String> collect1 = appleList.stream().map(Apple::getName).collect(Collectors.toList());
        System.err.println(collect1);
        Map<Integer, Apple> collect = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a, (k1, k2) -> k1));

        System.out.println("map结构"+collect);
        //{1=Apple(id=1, name=苹果1, money=3.25, num=10), 2=Apple(id=2, name=香蕉, money=2.89, num=30), 3=Apple(id=3, name=荔枝, money=9.99, num=40), 4=Apple(id=4, name=苹果2, money=1.35, num=20)}


        *//**
         * 过滤 Filter
         *//*
        //过滤出符合条件的数据
        List<Apple> filterList = appleList.stream().filter(a -> a.getName().equals("香蕉")).collect(Collectors.toList());

        System.err.println("filterList:" + filterList);
        //[Apple{id=2, name='香蕉', money=2.89, num=30}]

        *//**
         * 分组
         *//*
        Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));

        System.err.println("分组groupBy:" + groupBy);


        *//**
         * 分组求和
         *//*

        Map<Integer, Integer> groupAndSum = appleList.stream().collect(Collectors.groupingBy(Apple::getId, Collectors.summingInt(Apple::getNum)));
        System.err.println("totalMoney:" + groupAndSum);  //totalMoney:17.48*/
    }

    /**
     * 对象转map
     * @param object
     * @return
     */
    public static Map<String, String> ObjectToMap(Object object) {
        if (object == null) {
            return null;
        }
        Map<String, String> map = JSON.parseObject(JSON.toJSONString(object), Map.class);
        System.out.println(map);
        String name = map.get("name");
        System.out.println(name);
        return map;
    }

}